// pages/my_account/my_account.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    
    userInfo: {
      inputText:'',
      phoneNum:'',
      region: [],
      date:'', 
      Usex:'',
    }, 
    Uimage:'',
    Uid:'',
    array: ['男', '女'],
    index:0,
    customItem: '全部',
    color_region:'#666',
    color_sex:'#666',
    color_date:'#666',
  },
  
  save_userMessage:function(e){
    console.log(e)
    let that = this;
    that.setData({
      index:e.detail.value++
    })
   
    let value = this.data.userInfo;
    wx.setStorage({
      data: value,
      key: 'userInfo',
    })
    wx.setStorage({
      data: this.data.Uimage,
      key: 'Uimage',
    })
  
    wx.showToast({
      title: '已保存编辑',
      icon: 'success',
      duration: 2000
    })

    this.uploadData()  /* 将用户信息上传到数据库 */
  }, 
  uploadData:function() {  /* 将用户信息上传到 数据库 */
    let value = this.data.userInfo;
    // console.log(index);
    let that = this;
    wx.request({
      url: `${app.globalData.url}/users/my-home/my-account`,//写自己的服务器
      header: { "Content-Type": "application/x-www-form-urlencoded" },
      method: "POST",
      data: {
        Uid:this.data.Uid,
        Uname:value.inputText,
        Usex:1,
        Uday:value.date,
        Uimage:this.data.Uimage,
        Uphone:value.phoneNum,
        Uemail:'221982@qq.com',
        Pid:'',
        Uarea:value.region,
        Ulevel: 0
        
      },
      success: function () { console.log("success") },
      fail: function (res) {
         console.log("fail")
         console.log(res)
         
      }

  })
},
 
  onShow1:function() {
    
    const self = this;
    let userInfo = wx.getStorageSync('userInfo');
    let Uimage = wx.getStorageSync('Uimage');

    if (userInfo) {
      console.log(userInfo);
        self.data.userInfo = userInfo;
        self.data.Uimage = Uimage;
        self.data.Uimage = Uimage;

        self.setData({
          userInfo : self.data.userInfo,
          Uimage: self.data.Uimage
        })
    } 
},


onInputText(e) { /* 昵称 */
  this.setData({
    "userInfo.inputText": e.detail.value
  })
     
},
bindPhone(e) { /* 电话 */
  this.setData({
    "userInfo.phoneNum": e.detail.value
  })
     
},
  bindDateChange: function(e) {
    this.setData({
      "userInfo.date": e.detail.value,
      click_date:''
    })
  },
  bindSexChange: function(e){
    this.setData({
      "userInfo.Usex": e.detail.value,
      click_sex:''
    })
  },
  bindRegionChange: function (e) {
  
    this.setData({
      "userInfo.region": e.detail.value,
      click_region:''
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let Uid = wx.getStorageSync('Uid');
    let Uimage = wx.getStorageSync('Uimage');

    this.setData({
      Uid:Uid,
      Uimage:Uimage
    })
    this.importData();//  查询数据库中的数据

  },
  onShow: function (options) {
    let Uid = wx.getStorageSync('Uid');
    let Uimage = wx.getStorageSync('Uimage');

    this.setData({
      Uid:Uid,
      Uimage:Uimage
    })
    this.importData();//  查询数据库中的数据
    this.onShow1();
  },
  importData: function(){
    let that = this;
    wx.request({   /* 查询数据 */
      url: `${app.globalData.url}/users/my-home/my-account`, //这里填写你的接口路径
      header: { 
          'Content-Type': 'application/json'
      },
      data: {//这里写你要请求的参数
        Uid:that.data.Uid
      },
    
      success: function(res) {
      //这里就是请求成功后，进行一些函数操作
      // console.log(that.data.Uid);
        console.log(res.data);
        that.setData({
          Uimage:res.data[0].Uimage,
          "userInfo.inputText":res.data[0].Uname,
          Uid:res.data[0].Uid
        })
      }
    })
  },
 




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
